#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
using namespace std;

#define mm(a, n) memset(a, n, sizeof a)
#define mk(a, b) make_pair(a, b)

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int N = 2510;

struct Course{
    int idx;
    vector<string> stu;

    bool operator< (const Course & c1) const {
        return idx < c1.idx;
    }
    
}course[N];

int n, m;


inline void solution() {
    cin >> n >> m;
    for (int i = 0; i < m; i ++ ) {
        int id, cnt;
        cin >> id >> cnt;
        course[i].idx = id;
        for (int _ = 0; _ < cnt; _ ++ ) {
            string s;
            cin >> s;
            course[i].stu.push_back(s);
        }
    }
    sort(course, course + m);
    unordered_map<string, vector<int>> hash;
    for (int i = 0; i < m; i ++ ) {
        auto & stu = course[i].stu;
        int idx = course[i].idx;
        for (auto & x : stu) 
            hash[x].push_back(idx);
    }
    string query;
    for (int i = 0; i < n; i ++ ) {
        cin >> query;
        auto & mp = hash[query];
        cout << query << " " << mp.size();
        for (auto & x : mp)
            cout << " " << x;
        cout << endl;
    }  
    return ;

}

int main() {
    freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}